﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace STSdb.MTL.Utils.Files
{
    public static class FileLogger
    {
        static FileStream fs = null;
        static object lock_fs = new object();

        public static void ActivateFileLogger(string path)
        {
            string p = (path == String.Empty) ?
                @"D:\temp\STS\stat_" + DateTime.Now.Ticks + ".csv" : path;

            if (fs != null)
            {
                fs.Flush();
                fs.Close();               
            }
            fs = new FileStream(p, FileMode.Append, FileAccess.Write);
        }

        public static void WriteToFileLogger(byte[] bt)
        {
            fs.Write(bt, 0, bt.Count());
            fs.Flush();
        }

        public static void WriteToFileLoggerAscii(string a)
        {
            byte[] bt = System.Text.Encoding.ASCII.GetBytes(a);

            lock (lock_fs)
            {
                fs.Write(bt, 0, bt.Count());
                fs.Flush();
            }
        }

        public static void WriteToFileLoggerUTF8(string a)
        {
            byte[] bt = System.Text.Encoding.UTF8.GetBytes(a);

            lock (lock_fs)
            {
                fs.Write(bt, 0, bt.Count());
                fs.Flush();
            }
        }

        public static void WriteToFileLoggerAscii(string a, bool addLF)
        {
            WriteToFileLoggerAscii(a + ((addLF) ? "\r\n" : ""));            
        }

        public static void WriteToFileLoggerUTF8(string a,bool addLF)
        {
            WriteToFileLoggerUTF8(a + ((addLF) ? "\r\n" : ""));            
        }



        private static bool xstarted = false;
        public static void StartedStopped(bool started)
        {
            if (started && xstarted)
            {
                Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId + " collision");
            }

            xstarted = started;
        }

    }
}
